#include "mib_functions.h"
#include <stdlib.h>
#include <string.h>

const unsigned char get_snmp_oid_last_byte(unsigned char param_ID)
{
	unsigned char tableRowNumber__or__isScalar = 0;
	switch (param_ID)
	{
		case 1:    /* paramMessageID      //tableRMC >> entryRMC  */
		tableRowNumber__or__isScalar = 1;
		break;
		case 2:    /* paramUTCTime      //tableRMC >> entryRMC  */
		tableRowNumber__or__isScalar = 2;
		break;
		case 11:    /* errStatus      //picocell >> errStatus  */
		case 13:    /* temperature      //picocell >> temperature  */
		tableRowNumber__or__isScalar = 0;
		break;
		default:
		return SNMP_ERROR;
		break;
	};
	return tableRowNumber__or__isScalar;
}

const unsigned char get_snmp_object_type(unsigned char param_ID)
{
	unsigned char asn_type;
	switch (param_ID)
	{
		case 1:    /* paramMessageID      //tableRMC >> entryRMC  */
		case 2:    /* paramUTCTime      //tableRMC >> entryRMC  */
		asn_type = SNMP_ASN1_OC_STR;
		break;
		case 11:    /* errStatus      //picocell >> errStatus  */
		case 13:    /* temperature      //picocell >> temperature  */
		asn_type = SNMP_ASN1_INTEG;
		break;
		default:
		return SNMP_ERROR;
		break;
	};
	return asn_type;
}

static unsigned char get_getVarbindSize(unsigned char paramID)
{
	switch(paramID)
	{
		case 1:    /* paramMessageID      //tableRMC >> entryRMC  */
		return SNMP__paramMessageID__SIZE;
		break;
		case 2:    /* paramUTCTime      //tableRMC >> entryRMC  */
		return SNMP__paramUTCTime__SIZE;
		break;
		case 11:    /* errStatus      //picocell >> errStatus  */
		return SNMP__errStatus__SIZE;
		break;
		case 13:    /* temperature      //picocell >> temperature  */
		return SNMP__temperature__SIZE;
		break;
	}
}
static void get_getVarbind(unsigned char paramID, unsigned char **data, unsigned char *dataSize)
{
	switch(paramID)
	{
		case 1:    /* paramMessageID      //tableRMC >> entryRMC  */
		*dataSize = SNMP__paramMessageID__SIZE;
		*data = (unsigned char *)malloc(*dataSize * sizeof(unsigned char));
		memcpy(*data, SNMP__paramMessageID, *dataSize);
		break;
		case 2:    /* paramUTCTime      //tableRMC >> entryRMC  */
		*dataSize = SNMP__paramUTCTime__SIZE;
		*data = (unsigned char *)malloc(*dataSize * sizeof(unsigned char));
		memcpy(*data, SNMP__paramUTCTime, *dataSize);
		break;
		case 11:    /* errStatus      //picocell >> errStatus  */
		*dataSize = SNMP__errStatus__SIZE;
		*data = (unsigned char *)malloc(*dataSize * sizeof(unsigned char));
		memcpy(*data, &SNMP__errStatus, *dataSize);
		break;
		case 13:    /* temperature      //picocell >> temperature  */
		*dataSize = SNMP__temperature__SIZE;
		*data = (unsigned char *)malloc(*dataSize * sizeof(unsigned char));
		memcpy(*data, &SNMP__temperature, *dataSize);
		break;
	}
}

void get_getRespZip(const unsigned char* getRequestVarbind, unsigned char** getRespVarbind) //all Varbindes must be zipped
{
	TSzippedsnmp ts_zippedsnmp;
	memcpy(&ts_zippedsnmp, getRequestVarbind, sizeof(TSzippedsnmp));
	TSzippedsnmp *return_ts_zippedsnmp;
	const unsigned char* iterator_getRequestVarbind = getRequestVarbind + sizeof(TSzippedsnmp);
	unsigned char iterator_length = ts_zippedsnmp.length_bindedVariables;
	unsigned char respVarbindes_length = 0;
	unsigned char respVarbindes_count = 0;
	while(iterator_length > 0)
	{
		iterator_getRequestVarbind += 2; iterator_length -= 2;//discarding unit_ID
		respVarbindes_length += get_getVarbindSize(*iterator_getRequestVarbind);
		++iterator_getRequestVarbind; iterator_length -= 1;//discarding param_ID
		++respVarbindes_count;
	}
	*getRespVarbind = (unsigned char*)malloc(sizeof(TSzippedsnmp)+(respVarbindes_length * sizeof(unsigned char))+(respVarbindes_count * (2+1+1)/*(sizeof(unitID)+sizeof(paramID)+sizeof(data_length))*/));
	unsigned char* iterator_getRespVarbind = *getRespVarbind + sizeof(TSzippedsnmp);
	iterator_getRequestVarbind = getRequestVarbind + sizeof(TSzippedsnmp);
	iterator_length = ts_zippedsnmp.length_bindedVariables;
	unsigned char total_length = 0;
	while(iterator_length > 0)
	{
		memcpy(iterator_getRespVarbind, iterator_getRequestVarbind, 2); iterator_getRespVarbind += 2; //write and discard unitID
		total_length += 2;
		iterator_getRequestVarbind += 2; iterator_length -= 2;//discarding unit_ID
		unsigned char paramID = *iterator_getRequestVarbind; ++iterator_getRequestVarbind; iterator_length -= 1; //discarding param_ID
		switch(ts_zippedsnmp.command)
		{
			case SNMP_ASN1_PDU_GET_REQ:
			break;
			//case SNMP_ASN1_PDU_GET_NEXT_REQ:
			//break;
			//case SNMP_ASN1_PDU_GET_RESP:
			//break;
			//case SNMP_ASN1_PDU_SET_REQ:
			//break;
			//case SNMP_ASN1_PDU_TRAP:
			//break;
		}
		*iterator_getRespVarbind = paramID; ++iterator_getRespVarbind; ++total_length; //write and discard paramID
		unsigned char *data = 0;
		unsigned char dataSize = 0;
		get_getVarbind(paramID, &data, &dataSize);//the pointer data and dataSize variable would be initialized into this function
		*iterator_getRespVarbind = dataSize; ++iterator_getRespVarbind; ++total_length; //write and discard data_length
		memcpy(iterator_getRespVarbind, data, dataSize); iterator_getRespVarbind += dataSize;
		total_length += dataSize;
		free(data);
	}
	return_ts_zippedsnmp->version = SNMP_V1;
	return_ts_zippedsnmp->command = SNMP_ASN1_PDU_GET_RESP;
	return_ts_zippedsnmp->err_code = 0x00;
	return_ts_zippedsnmp->length_bindedVariables = total_length;
	iterator_getRespVarbind = *getRespVarbind;
	memcpy(iterator_getRespVarbind, return_ts_zippedsnmp, sizeof(TSzippedsnmp));
}
